2000 Outback overheating?

My 2000 Outback (low mileage about 54K) has developed the habit of apparently overheating, at least the temperature gauge indicates this. What actually happens is, the gauge will start to fluctuate wildly, first rising up to the red zone, but then dropping back down, sometimes restabilizing at the normal level. I happen to think it’s not physically possible for the engine temperature to fluctuate this wildly in a short time. There is a long history behind this.

At first, the engine ovwerheated in winter, I took it to the dealer and they said the head gasket needed replacing, however it was only one side. This was apparently under warranty so they only charged labor. Later, they replaced the other side (I forget now how the need for this was discovered). Some time later (last summer) the radiator apparently sprang a leak. By the time we noticed the engine was overheating, not only did the radiator need to be replaced, but the head gasket was blown and had to be replaced again. But no evident long-term damage to the engine. A short time later, the engine seemed to overheat again. This time, we took it in (to the mechanic who replaced the radiator and head gasket). The mechanic couldn’t determine an obvious cause for the overheating, but we agreed to replace the thermostat because a previous Subaru had once needed that. But later the symptom re-appeared. One theory put forward by the mechanic is that the fan is intermittently failing to spin, but he’s hesitant to do anything without further evidence. Night before last, the check engine light came on, maybe this will provide a clue to the problem.

So, is the engine really overheating (or, put another way, is the engine temperature fluctuating wildly for some reason) or is something wrong with the temperature gauge/sensor?