This applies to all npcs(not counting moves that enduce switch, such as u-turn), the only way is if they cant do anything(ex:opponent npc has all poison attack moves and you have a steel type) they will switch if they have a Pokemon to switch into.EDIT:There is also just chances where they are tactical b, as in swampert is a ground type, being four times effective to him, he swapped to claydol specifically because its ability, levitate, makes it immune to ground type moves. They started being tactical in xy, but in s gens they werent very tactical, the thing I put in my anwser applies in sixth gen to but they are tactical aswell, whereas previous gens only went with what I put down.