in this tuto, I'll show you how to use \Drupal\Core\Render\Element\VerticalTabs to group input elements according category. using '#type' => 'vertical_tabs' .
Add the following code inside a function where you want to create vertical tabs elements :
$form['contactform'] = [
'#type' => 'vertical_tabs',
];
$form['info'] = [
'#type' => 'details',
'#title' => 'Informations',
'#group' => 'contactform',
];
$form['info']['name'] = [
'#type' => 'textfield',
'#title' => $this->t('Name'),
];
$form['info']['message'] = [
'#type' => 'textarea',
'#title' => $this->t('Message'),
];
$form['publication'] = [
'#type' => 'details',
'#title' => $this->t('Publication'),
'#group' => 'contactform',
];
$form['publication']['publisher'] = [
'#type' => 'textfield',
'#title' => $this->t('Publisher'),
];
$form['publication']['publisher_info'] = [
'#type' => 'textarea',
'#title' => $this->t('Message'),
];
And this is my final output: