Combining two ideal voltage sources with an impedance between them
You can do that because they are all in series. The same current flows through all the components. The terminal voltage is equal to the sum of all the component voltages. Those equalities apply whatever the order of the components.

You would not be able to re-order them if there were any shunt components changing the topology from all-series. As there are always stray capacitances to ground in the real world, it's whether these are significant that controls whether you can do this re-ordering. At mains frequencies, you can, at microwave frequencies, you can't. At intermediate frequencies, you'd need to do the sums to see how much error you're introducing.