It depends on the case. If you can remove the side panel and easily get to the CPU backplate to remove it, then you probably can do the swap with the motherboard in the case. It is probably easier to do it with the motherboard out of the case.
If it's one that requires you to replace its mounting frame, you have to remove the motherboard to do so.
if that isn't needed, you don't need to remove the motherboard.
It depends on the case. If you can remove the side panel and easily get to the CPU backplate to remove it, then you probably can do the swap with the motherboard in the case. It is probably easier to do it with the motherboard out of the case.
Both @ScrewySqrl and I are technically correct. If you don't have to change the backplate then you can usually do it in the case. If you HAVE to change the backplate then it depends on the case. Some cases have a hole in the motherboard tray that lets you access the CPU backplate.