I tried heating up the electronic throttle module and a few other components and then starting the car from cold to see if the problem happened straight away. It didn't.
I also ran the car without the MAF but that made no change.
So I have kind of ruled out the ETM and the MAF at this stage.
I read through the description of the B5244S4 engine on Volvo's VIDA software (factory service instructions, parts catalogue and diagnostics) and came across a description of the EVAP system. Their description says that it only operates once the car comes up to normal operating temperature. As it connects to the intake manifold and could be the source of a vacuum leak I'm thinking that this is a strong possibility. The plan is to get the problem happening and disconnect the electrical connection to the EVAP valve and see if it makes a difference.
Other than that I might try heating the ECU and camshaft position sensor, but I don't have any other ideas. I have ordered the diagnostic hardware that works with VIDA so that I can get more detailed fault codes in the hope that it may lead me to the cause of this problem.
I mentioned before that I thought the ETM had been replaced because it had some blue paint marks on it that wreckers often use. Well, I now have noticed that a lot of components have the blue paint on them: alternator, ECU box, coolant hoses and probably more. I wonder if the engine was replaced and the paint marks are on there so they can check nothing was removed possibly for warranty purposes.