It will be a faulty Camshaft Position sensor, easy to replace - about £35.
The sensor checks the position of the camshafts with reference to the position of the Crank from its sensor when the ignition is first turned on, so that correct fuel timing is applied by the "brain" according to whether the first cylinder is on a compression stroke or an exhaust stroke (4 stroke engine)
Once established at the beginning the ECU carries on from this start point, and only needs the crank sensor to maintain engine timeing.
Cam sensors seem to fail in a way that makes them work when cold but not when hot.
Thus, having started from cold, the engine will run perfectly well until switched off.
If an attempt to restart is made whilst the engine (and sensor) is still warm it is unable to make that first reading and you'll just spin the starter in frustration.
Leave it for half an hour or so and the engine (and sensor) will have cooled enough for it to surprisingly work.