The routing condition is simple: If there is a non-electric locomotive in the convoy, is_electric is false. Otherwise, is_electric is true. In route search, only convoy that has at least one non-electric locomotive is allowed to take a route which has non-electrified section.
Say there are two parallel tracks, one electrified, one isn't. The electrified track is one tile longer. If you only check whether a convoy is allowed to go there, a mixed convoy would always route via the unelectrified track, even though that means the electric unit is merely baggage. This would be infuriating.
A routing penalty, as prissi suggests, might help. Though it probably ends up meaning that no unelectrified track is used unless absolutely necessary.
If a convoy with only electric locomotives tries to calculate route and there are no choice but to pass non-electrified section, the route search result must be no_route, that is same as current simutrans. This prevents electric convoys from moving at 1km/h in non-electrified section because a convoy cannot be disassembled outside a depot.
Except your convoy coupling patch kind of is all about assembling and disassembling outside a depot. What happens if an electric unit and a diesel locomotive merge, reach their next destination which happens to be unelectrified, and split there? You'd have to check the splitting point before they even merge, many stations in advance. Possible, I'm sure, but not... elegant.
One could handle it from a completely different angle. Start by changing what happens to convois without power - instead of still moving at 1km/h, just have them stop, but as soon as it happens, send out a warning to the player, and after a while, teleport that vehicle to the nearest depot*. Then, consider the potential speed in routing (I'm not sure if that's already done, but if so, it's only done for way speeds, not for vehicle speeds on ways).
This would mean two things: On one hand, the choice between the non-electrified and the electrified track would be smart enough even in less clear situations, such that even if something unintended happens, it's not something completely unexpected. (like trains driving across half the map because there technically is another route completely electrified, or trains going at a fraction of their speed due to using an unelectrified track for being slightly shorter)
On the other hand, you wouldn't have to bother with trains ending up unable to move, since even if it happens, the game handles them. Since speed becomes part of routing, any tile that causes a speed of 0 would be blocked.
As a side effect, the same logic could be used for weight limitations, not only for hard limit, but for soft limits as well - ie. not only can a track be passable or not depending on the weight, but it might limit speed depending on weight, and heavy vehicles can only go at fraction the normal nominal speed of the track. Which I, personally, would think a much more interesting feature than electric units on unelectrified tracks anyway

As you may know by now, this is just me spitting out ideas without worrying whether it can even be reasonably programmed. I guess my points are, mainly, that routing is very important in this matter, and that it might be better to change the foundation, rather than 'hacking' this in.
Also in real world, usually the engine is exchanged unless there are special construction works. Dual traction exist when the electric is just an extra push for a slope (that exist still in very few places, but the electric engine will uncouple after the slope). Because engines are heavy, and usually you do not want to haul them needlessly.
Note that THLeaderHs convoi coupling patch could allow for something similar to that: A convoi consisting of only a diesel locomotive could go between two stations that mark the beginning and end of an unelectrified track, waiting for electric units that need passage, without ever transporting anything itself. There can even be a reason for doing this in real games if you use another players tracks which he refuses to electrify.
Also, the only reason why his patch could not be used to exchange the engine in the first place is that a convoi without engine can't leave the depot. Otherwise, you could have a convoi without engine coupled with a convoi that's only an electric engine, until they disengage and the convoi without engine couples with a convoi consisting of a diesel engine. In the same way. You could have engine-less extra wagons that get detached like 3 stops before a route ends and wait until the engine comes back and they can attach again. Honestly, I think all that's really needed is
A) several convois need to be able to leave the depot pre-coupled (such that those engineless-convois can start somehow)
B) convois without engine, if not waiting for an engine in a station, need to be handled differently (not by going 1 km/h, but by being moved out of the way once it's clear they got stuck)
*the nearest depot, in this case, can be direct line without considering tracks. Maybe the wagons were hauled away by truck

It's just so they get out of the way and no longer block everything. Could be an option to activate in other vehicles as well ('if "no route" for too long, teleport to depot'). Clearly, that would not be realistic, but simply a gameplay measure.
It should be noted that what THLeaderH is saying and what others are saying is a completely different case.
What THLeaderH says is that diesel locomotive pulls EMU. Diesel locomotive (and generator vehicle) is added just before entering the non-electrified section to pull EMU. Then EMU can enter the non-electrified section. (Then EMU stops all motors.)
I think other people are talking about dual-mode or bi-mode and Prissi is talking about support locomotives for the slopes.
Yes, the use-cases are different, but the problems of mixed engines are the same.
I think in both cases the problem is in running costs rather than route searching. Because EMU needs a large investment in electrification originally, it should be characterized as a low running cost instead. [...] For dual-mode vehicles, different running costs need to be set in the electrification mode and the non-electrification mode.
You are right that dual-mode vehicles are a different topic and need different running costs depending on mode. However, because the electric mode is much cheaper, I think it should prefer electrified ways in routing still. Furthermore, they probably don't have the exact same power in both modes?