Fieldgroups for drupal 9 code example
Example: drupal 8 group fields custom form
// Gourp field in fieldset.
$form['group'] = [
'#type' => 'fieldset',
'#title' => $this->t('Group'),
'#collapsible' => FALSE,
'#collapsed' => FALSE,
];
$form['group']['field_1'] = [
'#type' => 'textfield',
'#title' => $this->t('Field 1'),
];
$form['group']['field_2'] = [
'#type' => 'textfield',
'#title' => $this->t('Field 2'),
];