I’d be looking to the sun sensor which get really gross and dirty. That is between the windscreen vent panels. That little flat bit. That receives the IR signal from the key to lock and unlock the door. It is separate from the security network (for want of a better term). I cleaned one up with CO Contact Cleaner.
@krusing replaced his and had everything working again.
If yours gets a bit iffy, put the key on the glass right at the sensor and it might open the car.
The sometimes doesn’t start thing, I’d be checking the contact of the pin on the key to the slip ring around the ignition barrel. If that looses contact when turning the key, or driving, it’s going to fail to start or run.
Slip rings are sometimes all grot inside and would benefit from a clean or replace them with minimal skill and cost.
Sometimes that pin on the key head gets pushed in a bit and contact is infrequent.
In both cases, I’d be looking to get a new key head and have it programmed in to the car. You don’t need new keys cut, but it’s nice to have the whole thing looking new.
I’ve gone down the path of aftermarket keys and had nothing but less money and a headache.