Toggling the show state of secure frames like unit frames and action bars can be problematic because if you do it in combat you will get errors.
Normally you would register the condition(s) you want them shown/hidden using RegisterStateDriver() and let the game look after them, like:
Code:
RegisterStateDriver(PlayerFrame, "visibility", "[petbattle] hide; default")
To hide when entering a pet battle and show again when leaving.