Cartman isn't as fixed about his friends/enemies as other characters are.
If anything, Cartman is the most capable (of all the characters) to switch his opinion based on what benefits him.
Using the "Ginger Cow" episode as an example, Cartman was trying to keep his prank about a ginger cow going at all costs. However, when he realized that Kyle actually wanted the story to hold true (due to creating world peace), Cartman switched sides and acted like he needed to come clean about the whole thing. Cartman much preferred to extort Kyle, as opposed to simply pulling off a prank. He changed his stance accordingly.
Similarly, although Cartman does seem to target Kyle more than Stan or Kenny, he still considers Kyle a friend (Cartman's definition of "friend" is different than most people's). You can see this in past actions:
- Cartman had no qualms siding with Kyle in the great Console Wars (the Game of Thrones parody episodes) because Cartman and Kyle both wanted an XBox over a PlayStation. Note: Cartman betrayed Kyle, but that wasn't related to Kyle. Cartman was literally betraying everyone every step of the way, Game of Thrones style.
- In the episode where Kyle moves to San Francisco, Cartman specifically travels to SF to save Kyle from the smog/smug storm. This was not an imaginary catastrophy, he actually saved his life.
- Cartman saved Kyle's life in the Imaginationland trilogy (after he was savaged by ManBearPig). Granted, it was because Kyle still needed to suck his balls; proving the point that Cartman only cares about what Cartman wants. Saving Kyle helped him get waht he wants, so he did it.
- I also remember Cartman saving people from the planetarium, IIRC Kyle was part of the rescued.
Overall, Cartman generally only picks on Kyle (or Kenny) when nothing else is going on. It's how he treats his friends by default (Stan is so average that he doesn't really have anything to get mocked about other than liking Wendy).
So why did cop Cartman stick up for cop Kyle?
We can't really know for sure, but there are reason why Cartman would stick up for Kyle (without breaking Cartman's character):
- Cartman was playing a cop, and therefore acted like a cop (regardless of the identity of the actors). This makes sense, especially because Cartman is using an overused cliché. The boys have a habit of losing themselves in their playacting (e.g. The Return of the Fellowship of the Ring to the Two Towers)
- Siding with Kyle was more beneficial, compared to siding with Stan; so he picked Kyle's side.
- Cartman had an active beef with Stan, which (in the moment) generally overrides his long-standing dislike of Kyle's Jewish nature.