Is it still possible to use a custom attribute for a form (e.g. $form['#foo'])? In the documentation for #value, the Drupal 7 documentation contains the following note:


  Note that as of Drupal 6, you can also simply store arbitrary variables in $form['#foo'] instead, as long as '#foo' does not conflict with any other internal property of the Form API.


Is that still possible, in Drupal 8?

I looked at the documentation for the Value class, but the note given for Drupal 7 is not present.

I know Drupal adds $form['#token'] to forms, but I take it is a special case Drupal handles in a particular way.
See FormCache::setCache(). The whole $form is put in the cache, anything that's in there is stored and fetched again.

That said, I would definitely advise against using it. The recommended and standard way to store something is $form_state->get()/set().

Also be aware that while this still works when the form is cached, Drupal 8 only caches form/form_state once an actual form submission happens. Usually that means only for ajax requests and in case of form validation errors (although I don't 100% know how it behaves then).

That means you can not put something in $form['#whatever'] or $form_state->set('whatever, ...) on the first form build and expect it to stay there.