The above code is going to create an infinite loop, because every time the customer is saved the event will be triggered and the observer will keep executing until it crashes.
Adding a stop condition should fix the issue:
$groupId = $this->_request->getParam('groupId');
if ($groupId !== $customer->getGroupId()) {
$customer->setGroupId($groupId);
$this->customerRepository->save($customer);
}
Alternatively, you can also use this extension: Customer group selector [M2]