There isn't anything fundamentally better. I might write it as follows:
// use a suitable name that describes the subset of projects
def myProjects = [project(":project1"), project(":project2")]
configure(myProjects) {
task helloTask << {
println "Run task"
}
}