The car can detect the presence of the key even to the extent of knowing if the key is inside the car or just outside the car .If the keys are still inside it wont allow 'keyless' locking. If its outside it may object if you leave the ignition active.
As Marcus has alluded to it could be related to how you carry your keys. The key transmits proximity signals to the dashboard area. If these signals are obstructed they may be weakened enough the signal is unreliable . Especially if the key battery is no longer 100%. . This could happen if the keys are in a handbag, deep down in a pocket, or obstructed by an object such as a metal spectacle case .
For safety reasons I dont think a momentary loss of signal shuts the car down whilst you are driving along , but could cause the electrical gremlins you describe.
I must admit I once forgot to remove my key from its fadaday bag and wondered why the car wouldnt start.

This bag is only thin metallised cloth but blocks 100% of key transmissions.
When a mechanic tests the car its more likely they will have the key in their hand and place it in a good reception location and wouldnt therefore detect any problem.