This is called an 'automatic transfer switch', it basically islands your off-grid installation when the power isn't available. As you already mention whether or not it is allowed is dependent on where you live. As it was the electrical inspectors in Northern Ontario were very much in bed with the power companies and typically would find all kinds of reasons to persuade renewable energy builders to let it go and rely on grid power. Given the state of some of the homebrew systems I've seen they probably had a point.
Automatic transfer switches were technically allowed but at the time I didn't know of a single installation in Northern Ontario that got a stamp of approval and was allowed to be operated un-attended in grid connected mode.
There is some risk, but this is usually exaggerated, the theory goes that a lineman should be able to disconnect a segment and then it should be idle, requiring all renewable installations down-grid to disable themselves. As a rule this is fully automatic anyway because all of the grid connected inverters that I'm aware of stop working as soon as the grid fails because they use the grid to supply them with phase and frequency information required to function.
But that wasn't good enough, the installation needed to be physically disconnected from the grid, requiring a very large relay and somehow it then had to reconnect if the grid came back on for a specified minimum amount of time (I forgot the details). By the time I had worked all this out I figured since I'm going to be in island mode anyway most of the time I may as well take the shortcut, cut the grid entirely (saving a considerable amount of money in the progress) and invest the difference into a nice genset (which I did, a 5.8KW Kubota diesel).