Add a "autocomplete" field in a buildForm
You use entity_autocomplete as #type for the form field. CommentForm::form() contains such a form field, for the administrator users to be able to change the author of a comment. This is the code it uses.

  // The uid field is only displayed when a user with the permission
  // 'administer comments' is editing an existing comment from an
  // authenticated user.
  $owner = $comment-&gt;getOwner();
  $form['author']['uid'] = [
    '#type' =&gt; 'entity_autocomplete',
    '#target_type' =&gt; 'user',
    '#default_value' =&gt; $owner-&gt;isAnonymous() ? NULL : $owner,
    // A comment can be made anonymous by leaving this field empty therefore
    // there is no need to list them in the autocomplete.
    '#selection_settings' =&gt; ['include_anonymous' =&gt; FALSE],
    '#title' =&gt; $this-&gt;t('Authored by'),
    '#description' =&gt; $this-&gt;t('Leave blank for %anonymous.', ['%anonymous' =&gt; $config-&gt;get('anonymous')]),
    '#access' =&gt; $is_admin,
  ];


As you can see from the code, you can also choose to include the anonymous user.

The result is shown in the following screenshot.