There was already a way to do that. Cmd-Shift-4, drag a rectangle around what you want to screencap. Cmd-Shift-4 Space, click a window to screencap it.
These have been in the OS for so long I can't even remember when they were added.
https://support.apple.com/en-us/102646