I second the advice on the grounding connections and especially the sensors. Years ago I had a somewhat similar experience. Not so much the overheating, but chronic (really chronic) ac failures. After replacing all (not kidding) of the ac components over time as each failed, the problem turned out to be the high pressure fan switch. My car didn't overheat, but since the fans intermittently didn't cut in the ac pressure went off the scale causing whatever was the weakest component in the system to fail. Learned alot about ac, but really didn't enjoy the process. Simple, and not too expensive to swap them out, which in this case isn't a bad diagnostic approach given the intermittent nature of the problem. But first clean those ground points. These electrical systems are fussy. It is amazing how many strange problems occur from poor fuse or relay contacts, even when they look ok to the eye.
2002 ML500, 2009 CLK350 Cabriolet
93 300D 2.5 Turbo 265,000 Miles (Sold - bad idea)