If it is an NPC then the DM is choosing who it attacks and what it is doing, when it fails morale and just flees and if something is against its nature or moral code or whatever.I have never understood this problem.
The game works great if you run the animal as a friendly NPC. If you want it to survive for a long time, allow it to gain some NPC type class levels to maintain survivability.
I've used this approach for decades - going so far as to make a 1st level spell for druids, rangers and nature clerics that sets the stages for the gaining HD - and it has never been problematic.
You can give a ranger class abilities that buff an animal ally or grants them abilities - that is fine ... but why do we have to put the animal under the fine control of the PC? The player is running one PC - the DM running the ally, the best, should be expected and sensible.
The current 5E design has pets under the absolute control of the PC.