How to set a custom class to submenus
Here's one way to modify your code to add a unique class for each menu level dynamically:


Add a new class appended with the value of menu_level plus one:


  {%
    set submenu_classes = [
      'sidebar__menu' ~ menu_name|clean_class ~ '--submenu',
      'sidebar__menu--submenu-' ~ (menu_level + 1),
    ]
  %}



Remove the previously added class, otherwise I believe it will add the class for each level incrementally. 


    {% if menu_level == 0 %}
      &lt;ul{{ attributes.addClass('container mx-auto', menu_classes) }}&gt;
    {% else %}
      &lt;ul{{ attributes.removeClass(menu_classes, 'sidebar__menu--submenu-' ~ (menu_level)).addClass(submenu_classes) }}&gt;
    {% endif %}