MacBook randomly shuts down
The symptom of powering off suddenly at various charge states (sometimes 25% left, sometimes 75% left) is almost always a battery that can’t keep up with the designed current. 

Some times it’s physical damage from a drop, sometimes chemical damage from being in a car trunk, sometimes just an out of spec battery. 

Any service technician with Apple diagnostics can diagnose and repair that for you. Be sure your firmware and software is up to date before paying for service, though.  Also, try once a SMC reset. 


https://apple.stackexchange.com/a/297277/5472
MacBook Pro randomly shutting down
Fully charged MacBook Air turns off when power cord removed


DIY repair is possible, especially if you are patient and aware an improper battery swap is a fire hazard and can afford a do over if you damage the battery when it’s out or don’t get the adhesive properly handled. 


Is it possible to replace a whole keyboard on a MacBook?